package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.TimeCount;
import com.chain.BrotherExchange.viewmodel.SecurityCenterViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import butterknife.Bind;

/**
 * 交易密码设置、修改
 */
public class SettingTransactionPasswordActivity extends BaseActivity {

    @Bind(R.id.send_text) TextView sendText;
    @Bind(R.id.auth_code_et) EditText authCodeEt;
    @Bind(R.id.affirm_button) View mAffirmButton;
    @Bind(R.id.mobile_phone_text) TextView mobilePhoneText;
    @Bind(R.id.new_password_edit) EditText newPasswordEdit;
    @Bind(R.id.affirm_new_password_edit) EditText affirmNewPasswordEdit;

    private SecurityCenterViewModel mViewModel;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, SettingTransactionPasswordActivity.class));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_setting_transaction_password;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(SecurityCenterViewModel.class);

        // 标题
        ActivityBarHelper.init(this,
                Androids.LocalConfig.isSetTransactionPassword()
                        ? R.string.title_modify_transaction_password
                        : R.string.title_setting_transaction_password
        );

        // 当前手机
        String phone = Androids.LocalConfig.getPhone();
        mobilePhoneText.setText(String.format(getString(R.string.current_mobile_phone_number), phone));

        // 发送短信
        sendText.setOnClickListener(v -> sendMessage());

        // 修改或设置
        mAffirmButton.setOnClickListener(v -> modifyPassword());
    }

    private void sendMessage() {
        mViewModel.sendMessage(C.API_MODIFY_TRANSACTION_PASSWORD_TYPE).observe(this, resource -> {
            if (resource.state == Resource.Status.LOADING) {
                Androids.showProgressMessage(this, R.string.sending_verification_code);
            } else {
                Androids.dismissProgressMessage();
                Androids.makeText(resource.message);

                if (resource.state == Resource.Status.SUCCESS) {
                    new TimeCount(sendText).start();
                }
            }
        });
    }

    private void modifyPassword() {
        String code = authCodeEt.getText().toString();
        String newPsd = newPasswordEdit.getText().toString();
        String affirmPsd = affirmNewPasswordEdit.getText().toString();
        mViewModel.setOrModifyTransactionPassword(code, newPsd, affirmPsd).observe(this, resource -> {
                    if (resource.state == Resource.Status.LOADING) {
                        Androids.showProgressMessage(this,
                                Androids.LocalConfig.isSetTransactionPassword()
                                        ? R.string.modifying
                                        : R.string.setting_in);
                    } else {
                        Androids.dismissProgressMessage();
                        Androids.makeText(resource.message);

                        if (resource.state == Resource.Status.SUCCESS) {
                            Androids.LocalConfig.setAlreadySetTransactionPassword(true);
                            finish();
                        }
                    }
                }
        );
    }
}
